package com.blb.community_admin.controller;

import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysPost;
import com.blb.community_admin.entity.SysUser;
import com.blb.community_admin.entity.SysUserPost;
import com.blb.community_admin.service.ISysPostService;
import com.blb.community_admin.service.ISysUserPostService;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags ="添加userPost")
public class SysUserPostController {

    @Resource
    private ISysUserPostService sysUserPostService;

    @Resource
    private ISysPostService sysPostService;

    @Log(module = "用户岗位管理", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加用户Post")
    @PostMapping("/users/addpost")
    public ResponseResult<String> addpost(Long postId,@RequestBody SysUser sysUser){
        SysUserPost sysUserPost = new SysUserPost(sysUser.getUserId(),postId);
        sysUserPostService.save(sysUserPost);
        return ResponseResult.ok("添加成功");
    }

    @ApiOperation("查询用户post")
    @GetMapping("/users/postid/{userId}")
    public ResponseResult<SysPost> byPostId(@PathVariable Long userId){
        SysUserPost sysUserPost = sysUserPostService.getById(userId);
        SysPost syspost = sysPostService.getById(sysUserPost.getPostId());
        return ResponseResult.ok(syspost);
    }

    @Log(module = "用户岗位管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("根据userid修改postid")
    @PutMapping("/users/updatepost/{post}")
    public ResponseResult<String> updateUser(@PathVariable Long post,@RequestBody SysUser sysUser){
        SysUserPost sysUserPost = new SysUserPost(sysUser.getUserId(), post);
        sysUserPostService.updateById(sysUserPost);
        return ResponseResult.ok("修改成功");

    }

}
